/**   
 * @Title: AbsServerCommunicater.java 
 * @Package com.esun.cheetah.view.news
 * @Description: 通讯
 * @author 程相亭 <br/>
 * @date 2012-03-14 下午05:07:33  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.cheetah.network;

import java.io.IOException;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParserException;

import com.esun.cheetah.config.Config;
import com.esun.cheetah.config.Key;
import com.esun.util.StringUtil;
import com.esun.util.encryption.Encryption;
import com.esun.util.log.EDebug;

/**
 * @ClassName: AbsServerCommunicater
 * @Description:主通讯类
 * @author 程相亭 <br/>
 * @date 2012-03-14 下午05:07:33
 */
public abstract class AbsServerCommunicater {

	private Object obj = new Object();
	private String serverMsg = null;

	public void isGetServerResponseSuccess(HashMap<String, String> hmJson) {

		// 组装json
		String strJson = StringUtil.comJson(hmJson);
		EDebug.o("------------------------------------->传入服务器参数：\n" + strJson);
		try {
			EDebug.o("------------------------------------->服务器通讯开始：");
			String serverRes = communicateWithServer(hmJson.get(Key.KEY_ID),
					strJson);
			EDebug.o("------------------------------------->服务器返回内容：\n"
					+ serverRes);
			EDebug.o("------------------------------------->服务器通讯结束："
					+ hmJson.get(Key.KEY_ID));

			// 解析调用是否成功
			JSONObject jsonObject = new JSONObject(serverRes);
			String status = jsonObject.getString(Key.KEY_STATUS);

			if (status != null && status.equals(Key.KEY_TRUE)) {

				// 获取json信息
				parseJson(serverRes);
				getServerResponseSuccess();

			} else {
				// 获取失败处理
				setServerMsg(jsonObject.getString(Key.KEY_MSG));
				getServerResponseFail(serverRes);
			}
		} catch (Exception e) {
			// 异常处理
			getServerResponseException();
			EDebug.o("------------------------------------->服务器通讯发生异常" + e);
		}
	}

	/**
	 * @throws XmlPullParserException
	 * @throws IOException
	 * @Title: communicateWithServer
	 * @Description: 主通讯类
	 * @param
	 * @return String 服务器返回json串
	 * @throws
	 */
	private String communicateWithServer(String id, String strJson)
			throws IOException, XmlPullParserException {

		SoapObject soapObject = new SoapObject(Config.NAME_SPACE,
				Config.METHOD_NAME);
		// 添加参数
		soapObject.addProperty(Key.KEY_ID, id);
		soapObject.addProperty(Key.KEY_ARRAY, encryption(strJson));

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER10);
		envelope.bodyOut = soapObject;
		envelope.dotNet = true;
		envelope.setOutputSoapObject(soapObject);

		// 通信调用，设置超时
		EsunAndroidHttpTransport androidHttpTransport = new EsunAndroidHttpTransport(
				Config.POST_ADDRESS, Config.TIME_OUT_CONN, Config.TIME_OUT_READ);
		androidHttpTransport.debug = true;

		androidHttpTransport.call(null, envelope);

		Object res = envelope.getResponse();
		// 返回json串
		return res.toString();

	}

	/**
	 * @throws JSONException
	 * @Title: doSomeStuff
	 * @Description: 主解析类
	 * @param
	 * @return Object json解析完成后对象
	 * @throws
	 */
	abstract public void parseJson(String serverRes) throws JSONException;

	/**
	 * @throws JSONException 
	 * @throws
	 * @Title: getServerResponseFail
	 * @Description: 获取失败操作
	 * @param
	 * @return
	 * @throws
	 */
	abstract public void getServerResponseFail(String serverRes) throws JSONException;

	/**
	 * @throws
	 * @Title: getServerResponseSuccess
	 * @Description: 获取成功操作
	 * @param
	 * @return
	 * @throws
	 */
	abstract public void getServerResponseSuccess();

	/**
	 * @throws
	 * @Title: getServerResponseException
	 * @Description: 获取发生异常操作
	 * @param
	 * @return
	 * @throws
	 */
	abstract public void getServerResponseException();

	/**
	 * @throws
	 * @Title: encryption
	 * @Description: 加密预留接口
	 * @param
	 * @return String 加密后字符串
	 * @throws
	 */
	public String encryption(String orgStr) {
		
		Encryption encryption = new Encryption() ;
		return encryption.doEncryption(orgStr) ;
		
	}
	
	public Object getObj() {
		return this.obj;
	}

	public void setObj(Object obj) {
		this.obj = obj;
	}

	public String getServerMsg() {
		return this.serverMsg;
	}

	public void setServerMsg(String serverMsg) {
		this.serverMsg = serverMsg;
	}
}
